// component/relations/relations.js
import {
  FANS_LIST,
  FOLLOWS_LIST,
  BLACKLIST,
} from "../../request/api";

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    listData: {
      type: Object,
      observer: function (newVal, oldVal) {
        if(newVal.height){
          this.setData({
            listViewData: {
              height: newVal.height,
            }
          });
        }
        if(newVal.type && newVal.userId){
          this.data.type = newVal.type;
          this.data.userId = newVal.userId;
          if(this.data.dataList.length == 0){
            this.loadMore();
          }
        } else if(newVal.type){
          this.data.type = newVal.type;
        } else if(newVal.userId){
          this.data.userId = newVal.userId;
          if(this.data.dataList.length == 0){
            this.loadMore();
          }
        }
        
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    dataList: [],
    pageToken: '',
    type: '1',
    userId: '',
    isMyRelation: false,
    listViewData: {
      loadStatus: 2,
    }
  },

  lifetimes: {
    ready: function(){
      if(this.data.dataList.length == 0){
        this.loadMore();
      }
    }
  },

  // 组件所在页面的生命周期函数
  pageLifetimes: {
    show: function () {
      // this.loadMore();
    },
    hide: function () {},
    resize: function () {},
  },

  /**
   * 组件的方法列表
   */
  methods: {
    refresh: function () {
      this.data.pageToken = '';
      this.data.dataList = [];
      this.loadMore();
    },
    loadMore: function () {
      if(this.data.listViewData.loadStatus == 1){
        return;
      }
      if(!this.data.isMyRelation && !this.data.userId){
        return;
      }
      this.data.listViewData.loadStatus = 1;
      var userId = this.data.userId;
      var pageToken = this.data.pageToken;
      switch (this.data.type) {
        case 1:
          this.loadFollows({
            'id': userId,
            'pageToken': pageToken
          })
          break;
        case 2:
          this.loadFans({
            'id': userId,
            'pageToken': pageToken
          })
          break;
        case 3:
          this.loadBlacklist({
            'id': userId,
            'pageToken': pageToken
          })
          break;
      }
    },

    loadFans: function (data) {
      FANS_LIST(data).then(res => {
        this.setListData(res);
      }).catch(err => {
        this.setLoadStatus(2);
      });
    },
    loadFollows: function (data) {
      FOLLOWS_LIST(data).then(res => {
        this.setListData(res);
      }).catch(err => {
        this.setLoadStatus(2);
      });
    },
    loadBlacklist: function (data) {
      BLACKLIST(data).then(res => {
        this.setListData(res);
      }).catch(err => {
        this.setLoadStatus(2);
      });
    },

    setListData: function (res) {
      if (res.data.code == 1) {
        var tempList = this.data.dataList;
        var result = res.data.result;
        if (this.data.pageToken.length == 0) {
          tempList = result.items;
        } else {
          tempList = tempList.concat(result.items);
        }
        this.setData({
          dataList: tempList,
          pageToken: result.nextPageToken,
          listViewData: {
            loadStatus: result.requestCount > result.responseCount? 3 : 2,
          },
        });
      } else {
        this.setLoadStatus(2);
      }
    },

    setLoadStatus: function (status) {
      this.setData({
        listViewData: {
          loadStatus: status,
        },
      });
    },
  },
})